package tech.wangpai.controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.wangpai.config.swagger.ParamType;
import tech.wangpai.domain.User;
import tech.wangpai.service.UserService;
import tech.wangpai.config.swagger.DataType;

import java.util.List;

/**
 * 参考资料：https://github.com/chenjianfei-dl/swagger-spring-boot
 */
@Api(value = "用户接口", protocols = "http", tags = "用户接口")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户列表", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 401, message = "认证错误"),
            @ApiResponse(code = 404, message = "请求路径错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = DataType.STRING, paramType = ParamType.QUERY)
    })
    @GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<List<User>> queryUser(String username) {
        return new ResponseEntity(userService.queryUser(username), HttpStatus.OK);
    }

    @ApiOperation(value = "根据ID查询用户信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 401, message = "认证错误"),
            @ApiResponse(code = 404, message = "请求路径错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = DataType.STRING, paramType = ParamType.PATH)
    })
    @GetMapping(value = "/user/{userId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getUserById(@PathVariable(value = "userId") String userId) {
        return new ResponseEntity(userService.getUserById(userId), HttpStatus.OK);
    }

    @ApiOperation(value = "添加用户", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 401, message = "认证错误"),
            @ApiResponse(code = 404, message = "请求路径错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @PostMapping(value = "/user", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity addUser(@RequestBody User user) {
        userService.addUser(user);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "删除用户", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 401, message = "认证错误"),
            @ApiResponse(code = 404, message = "请求路径错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = DataType.STRING, paramType = ParamType.PATH)
    })
    @DeleteMapping(value = "/user/{userId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity deleteUserById(@PathVariable(value = "userId") String userId) {
        userService.delUserById(userId);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "更新用户", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 401, message = "认证错误"),
            @ApiResponse(code = 404, message = "请求路径错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = DataType.STRING, paramType = ParamType.PATH)
    })
    @PutMapping(value = "/user/{userId}", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE,MediaType.APPLICATION_JSON_UTF8_VALUE}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity updateUserById(@PathVariable(value = "userId") String userId, @RequestBody User user) {
        userService.updateUserById(userId, user);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "更新用户年龄", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数有误"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 401, message = "认证错误"),
            @ApiResponse(code = 404, message = "请求路径错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号", dataType = DataType.STRING, paramType = ParamType.PATH),
            @ApiImplicitParam(name = "age", value = "用户年龄", dataType = DataType.STRING, paramType = ParamType.PATH),
    })
    @PatchMapping(value = "/user/{userId}/{age}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity updateUserAgeById(@PathVariable(value = "userId") String userId, @PathVariable(value = "age") Integer age) {
        userService.updateUserAgeById(userId, age);
        return new ResponseEntity(HttpStatus.OK);
    }

}
